package com.life.wd.service.Imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.life.wd.dto.CommunityPostAddRequest;
import com.life.wd.dto.CommunityPostQueryRequest;
import com.life.wd.entity.CommunityPosts;
import com.life.wd.mapper.CommunityPostMapper;
import com.life.wd.mapper.UserMapper;
import com.life.wd.result.PageResult;
import com.life.wd.service.CommunityPostService;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class CommunityPostServiceImpl implements CommunityPostService {

    @Autowired
    private CommunityPostMapper communityPostMapper;
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer addPost(CommunityPostAddRequest request) throws IOException {
        // 验证用户是否存在
        validateUserExists(request.getUserId());

        // 构建帖子实体
        CommunityPosts post = new CommunityPosts();
        post.setUserId(request.getUserId());
        post.setTitle(request.getTitle());
        post.setContent(request.getContent());

        // 处理上传的封面图片
        MultipartFile file = request.getCoverFile();
        if (file != null && !file.isEmpty()) {
            post.setCover(file.getBytes());
        }

        // 设置默认值
        post.setViewCount(0);
        post.setReplyCount(0);
        post.setLikeCount(0);

        // 插入数据库
        communityPostMapper.insert(post);
        
        return post.getPostId();
    }

    // 验证用户存在性
    private void validateUserExists(Long userId) {
        // 实际实现中调用UserService验证用户是否存在
        if (userMapper.getById(userId) == null) {
            throw new IllegalArgumentException("用户不存在，ID: " + userId);
        }
    }

    @Override
    public PageResult listPosts(CommunityPostQueryRequest request) {
        // 设置分页参数
        PageHelper.startPage(request.getPageNum(), request.getPageSize());


        // 执行查询
        Page<CommunityPosts> posts = communityPostMapper.listPosts(request);

        // 构建分页结果
        PageInfo<CommunityPosts> pageInfo = new PageInfo<>(posts);
        return new PageResult(pageInfo.getTotal(), posts.getResult());
    }

    @Override
    public CommunityPosts getPostById(Integer postId) {
        return communityPostMapper.getPostById(postId);
    }


    @Override
    public boolean deletePost(Integer postId) {
        // 验证帖子是否存在
        if (communityPostMapper.getPostById(postId) == null) {
            throw new IllegalArgumentException("帖子不存在，ID: " + postId);
        }

        // 执行删除操作
        int rows = communityPostMapper.deletePost(postId);
        return rows > 0;
    }

    @Override
    @Transactional // 保证批量操作的原子性
    public boolean deletePostsBatch(List<Integer> postIds) {
        if (postIds == null || postIds.isEmpty()) {
            return false;
        }

        int total = postIds.size();
        int deleted = communityPostMapper.deletePostsBatch(postIds);
        return deleted == total;
    }

}